package com.lyz.file.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.log4j.Logger;

import com.lyz.config.ConfigFactory;
import com.lyz.config.IConfig;
import com.lyz.file.IFileProcess;
import com.lyz.util.PropertiesUtil;

public class LogFileProcess implements IFileProcess {

	private final static Logger logger = Logger.getLogger(LogFileProcess.class);

	private File file = null;
	private RandomAccessFile raf = null;
	private String fileName = null;
	private long position = 0;
	// max length of content in process file
	private int maxLength = 0;
	private IConfig config = null;
	
	private String tag = null;


	/**
	 * 
	 */
	public LogFileProcess(String tag) {
		this.tag = tag;
		config = ConfigFactory.getConfig();
		String confFilePath = config.getProcessingFilePath(tag);
		file = new File(confFilePath);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			raf = new RandomAccessFile(file, "rw");
			String line = raf.readLine();
			if (line == null || "".equals(line.trim())) {
				fileName = "";
				position = 0;
				maxLength = 0;
			} else {
				String[] tmp = line.trim().split(" ");
				fileName = tmp[0];
				position = Long.parseLong(tmp[1].trim());
				maxLength = line.length();
			}

		} catch (FileNotFoundException e) {
			logger.error("can not create processingFile", e);
		} catch (IOException e) {
			logger.error("error to new instance of GPSFileProcess", e);
		}
	}
	

	@Override
	public long getNextPosition() {
		return position;
	}

	@Override
	public void updateNextPosition(long position) {
		this.position = position;
		overrideFileContent();
	}

	// override process file content,ensure all the content can be replaced
	private void overrideFileContent() {
		try {
			String line = fileName + " " + position;
			raf.seek(0);
			if (line.length() >= maxLength) {
				raf.writeBytes(line);
				maxLength = line.length();
			} else {
				StringBuffer buffer = new StringBuffer(maxLength);
				buffer.append(line);
				for (int i = line.length(); i < maxLength; i++) {
					buffer.append(" ");
				}
				raf.writeBytes(buffer.toString());
			}

		} catch (IOException e) {
			logger.error("", e);
		}
	}

	@Override
	public String getNextFile() {
		return fileName;
	}

	@Override
	public void updateNextFile(String fileName) {

		if (fileName.equals(this.fileName)) {
			return;
		} else {
			this.fileName = fileName;
			this.position = 0;
			overrideFileContent();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		LogFileProcess ext = new LogFileProcess("vvlog");
		ext.updateNextFile("111.log");
		ext.updateNextFile("11.log");

		ext.updateNextPosition(12345);
		ext.updateNextPosition(1192509025);
		ext.updateNextPosition(12346);
		ext.updateNextPosition(1192509027);
		int count = 0;
		long pos = 1192509027;
//		while(count < 10000){
//			ext.updateNextPosition(pos++);
//		}
		ext.updateNextPosition(1);
//		ext.updateNextFile("222.log");

	}
}
